package com.ra.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ra.config.security.CustomFilter;
import com.ra.constant.CommonConstant;
import com.ra.exception.BizException;
import com.ra.mapper.UserRoleMapper;
import com.ra.pojo.*;
import com.ra.mapper.RoleMapper;
import com.ra.pojo.dto.ConditionDTO;
import com.ra.pojo.dto.RoleDTO;
import com.ra.service.IRoleMenuService;
import com.ra.service.IRoleResourceService;
import com.ra.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ra.service.IUserRoleService;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 今晚几点睡_Ada
 * @since 2023-03-09
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private IRoleMenuService roleMenuService;
    @Autowired
    private IRoleResourceService roleResourceService;
    @Autowired
    private CustomFilter customFilter;
    @Autowired
    private UserRoleMapper userRoleMapper;
   /* @Override
    public List<String> listRolesByUserInfoId(Integer userInfoId) {
        return roleMapper.listRolesByUserInfoId(userInfoId);
    }*/
    @Transactional
    @SneakyThrows
    @Override
    public void saveOrUpdateRole(RoleDTO roleDTO) {
        Role role = roleMapper.selectOne(new LambdaQueryWrapper<Role>().select(Role::getId).eq(Role::getRoleName,roleDTO.getRoleName()));
        if (Objects.nonNull(role) && !(role.getId().equals(roleDTO.getId()))) {
            throw new BizException("该角色存在");
        }
        //创建或更新
        Role role1 = Role.builder()
                .id(roleDTO.getId())
                .roleName(roleDTO.getRoleName())
                .isDisable(CommonConstant.FALSE)
                .build();
        saveOrUpdate(role1);
        //菜单
        if (Objects.nonNull(roleDTO.getMenuId())) {
            //如果是更新先移除原有的菜单项
            if (Objects.nonNull(roleDTO.getId())) {
            roleMenuService.remove(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId,roleDTO.getId()));
            }
            List<RoleMenu> collect = roleDTO.getMenuId().stream().map(item -> RoleMenu.builder()
                    .roleId(role.getId()).menuId(item).build()).collect(Collectors.toList());
            roleMenuService.saveBatch(collect);
        }
        //资源权限
        if (Objects.nonNull(roleDTO.getResourceId())) {
            //如果是更新先移除原有的资源项
            if (Objects.nonNull(roleDTO.getId())) {
                roleResourceService.remove(new LambdaQueryWrapper<RoleResource>().eq(RoleResource::getRoleId,roleDTO.getId()));
            }
            List<RoleResource> collect = roleDTO.getResourceId().stream().map(item -> RoleResource.builder()
                    .roleId(role.getId()).resourceId(item).build()).collect(Collectors.toList());
            roleResourceService.saveBatch(collect);
            customFilter.clearDataSource();
        }



    }

    @SneakyThrows
    @Override
    public void deleteRoles(List<Integer> roleId) {
        Integer count = userRoleMapper.selectCount(new LambdaQueryWrapper<UserRole>()
                .in(UserRole::getRoleId, roleId));
        if (count > 0) {
            throw new BizException("该角色下存在用户");
        }
        roleMapper.deleteBatchIds(roleId);
    }

    @Override
    public RespPageBean<RoleDTO> listRoles(ConditionDTO conditionDTO) {


        List<RoleDTO> roleDTOS = roleMapper.listRoles(conditionDTO);
        roleDTOS.forEach(System.out::println);

        Integer count =   roleMapper.selectCount(new LambdaQueryWrapper<Role>()
                .like(StringUtils.isNotBlank(conditionDTO.getKeywords()), Role::getRoleName, conditionDTO.getKeywords()));


        return new RespPageBean<>(Long.parseLong(count.toString()),roleDTOS);
    }
}

